package com.siysoft.tc.teacher.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.siysoft.tc.base.bean.bo.AssessmentBo;
import com.siysoft.tc.base.bean.bo.MRDetailBo;
import com.siysoft.tc.base.bean.bo.PTDetailBo;
import com.siysoft.tc.base.bean.bo.PaginationBo;
import com.siysoft.tc.base.bean.bo.ReservationHistoryBo;
import com.siysoft.tc.base.bean.bo.TeacherBo;
import com.siysoft.tc.base.bean.bo.TeacherDetailBo;
import com.siysoft.tc.base.bean.property.ResourceType;
import com.siysoft.tc.base.bean.to.Complain;
import com.siysoft.tc.base.bean.to.EvaluateStudent;
import com.siysoft.tc.base.bean.to.Resource;
import com.siysoft.tc.base.bean.to.User;
import com.siysoft.tc.base.bean.to.VTeacher;
import com.siysoft.tc.base.bean.to.VUserReservation;
import com.siysoft.tc.teacher.service.ITeacherService;
import com.siysoft.util.COMMON;
import com.tc.base.dao.BaseDAO;

@Service
public class TeacherServiceImpl implements ITeacherService{
	@Autowired
	BaseDAO baseDao;

	public String getResource(int data_id, String type) {
		String hql= "from Resource a where a.dataType='"+type+"' AND a.dataId="+data_id;
		List<Resource> resourcelist =baseDao.findByHQL(hql);
		Resource resource1=new Resource();
		if(resourcelist.size()!=0){
			return resourcelist.get(0).getImgPath();
		}
		return null;
		
	}
	
	@Override
	public List<TeacherBo> getAll(PaginationBo pb, String school) {
		// TODO Auto-generated method stub
		try{
			String hql = "from VTeacher where user_school='"+school+"' order by user_recommend_index desc";
			List<VTeacher> user = (List<VTeacher>)baseDao.findByHQL(hql, null, pb.getPagination_offset(), pb.getPagination_limit());
			pb.setPagination_total(baseDao.findByHQL(hql).size());
			List<TeacherBo> tb = new ArrayList<TeacherBo>();
			for(Integer i = 0; i < user.size(); i++){
				VTeacher u = user.get(i);
				int teacher_id = u.getUser_id();
				String teacher_name = u.getUser_name();
				String teacher_grade = u.getUser_grade();
				String teacher_department = u.getTeacher_department();
				String teacher_star = u.getTeacher_level();
				String teacher_avatar = COMMON.NulltoAvatar(this.getResource(teacher_id,ResourceType.USER_AVATAR));
				Integer teacher_goal = u.getTeacher_goal();
				Integer teacher_level_type = u.getTeacher_level_type();
				TeacherBo t = new TeacherBo(teacher_id, teacher_name,
						teacher_department, teacher_star, teacher_grade,
						teacher_avatar, teacher_goal, teacher_level_type);
				tb.add(t);
			}
			return tb;
		}catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	@Override
	public List<TeacherBo> getTeacher(String lecture, 
			String course, String school, PaginationBo pb) {
		// TODO Auto-generated method stub
		try{
			String hql = "from VTeacher where user_school='"+school+"'";
			Integer flag = 0;
			if(!lecture.equals("")){
				hql += " and user_name like '%" + lecture + "%'";
			}
			if(!course.equals("")){
				hql += " and teacher_course='" + course + "'";
			}
			hql += " order by user_recommend_index desc";
			List<VTeacher> user = (List<VTeacher>)baseDao.findByHQL(hql, null, pb.getPagination_offset(), pb.getPagination_limit());
			pb.setPagination_total(baseDao.findByHQL(hql).size());
			List<TeacherBo> tb = new ArrayList<TeacherBo>();
			for(Integer i = 0; i < user.size(); i++){
				VTeacher u = user.get(i);
				int teacher_id = u.getUser_id();
				String teacher_name = u.getUser_name();
				String teacher_grade = u.getUser_grade();
				String teacher_department = u.getTeacher_department();
				String teacher_star = u.getTeacher_level();
				String teacher_avatar = COMMON.NulltoAvatar(this.getResource(teacher_id,ResourceType.USER_AVATAR));
				Integer teacher_goal = u.getTeacher_goal();
				Integer teacher_level_type = u.getTeacher_level_type();
				TeacherBo t = new TeacherBo(teacher_id, teacher_name,
						teacher_department, teacher_star,
						teacher_grade, teacher_avatar, teacher_goal, teacher_level_type);
				tb.add(t);
			}
			return tb;
		}catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public TeacherDetailBo getDetail(Integer id, PaginationBo reservation_pb, PaginationBo assessment_pb) {
		// TODO Auto-generated method stub
		try {
			String hql1 = "from User where user_id="+id;
			List<User> user = baseDao.findByHQL(hql1);
			
			String hql2 = "from VUserReservation where user_id="+id;
			List<VUserReservation> user_reservation;
			
			user_reservation = (List<VUserReservation>) baseDao.findByHQL(hql2, null, reservation_pb.getPagination_offset(), reservation_pb.getPagination_limit());
			reservation_pb.setPagination_total(baseDao.findByHQL(hql2).size());
			List<ReservationHistoryBo> reservation_history = new ArrayList<ReservationHistoryBo>();
			for(Integer i = 0; i < user_reservation.size(); i++){
				VUserReservation vur = user_reservation.get(i);
				int reservation_history_id = vur.getReservation_id();
				String reservation_history_number = vur.getReservation_number();
				String reservation_history_course = vur.getReservation_course();
				Long reservation_history_time = vur.getReservation_time().getTime();
				Integer reservation_history_goal = vur.getReservation_goal();
				String reservation_history_teacher_name = vur.getReservation_lecture();
				ReservationHistoryBo rhb = new ReservationHistoryBo(reservation_history_id,
						reservation_history_number, reservation_history_course, reservation_history_time,
						reservation_history_goal, reservation_history_teacher_name);
				reservation_history.add(rhb);
			}
			
			String hql3 = "from EvaluateStudent where evaluate_teacher_id="+id;
			List<EvaluateStudent> les = baseDao.findByHQL(hql3);
			assessment_pb.setPagination_total(baseDao.findByHQL(hql3).size());
			List<AssessmentBo> assessment = new ArrayList<AssessmentBo>();
			
			for(Integer i = 0; i < les.size(); i++){
				EvaluateStudent es = les.get(i);
				Integer assessment_id = es.getEvaluate_id();
				Integer assessment_sender_id = es.getEvaluate_user_id();
				User tmp_u = (User)baseDao.findById(User.class, assessment_sender_id);
				String assessment_user_name = tmp_u.getUser_name();
				Timestamp assessment_time = es.getEvaluate_time();
				String assessment_content = es.getEvaluate_content();
				String assessment_user_avatar = COMMON.NulltoAvatar(this.getResource(assessment_sender_id,ResourceType.USER_AVATAR));
				AssessmentBo ab = new AssessmentBo(assessment_id, assessment_user_name, 
						assessment_time, assessment_content, assessment_user_avatar);
				assessment.add(ab);
			}
			User us = user.get(0);
			String teacher_detail_avatar = COMMON.NulltoAvatar(this.getResource(us.getUser_id(),ResourceType.USER_AVATAR));
			TeacherDetailBo tdb_return = new TeacherDetailBo(us.getUser_id(), us.getUser_name(), 
					teacher_detail_avatar, us.getUser_school(), us.getUser_student_id(), us.getUser_email(),
					us.getUser_telephone(), us.getTeacher_wage(), us.getTeacher_introduce(),
					reservation_history, reservation_pb, assessment, assessment_pb,
					us.getTeacher_experience(), us.getTeacher_pt(), us.getTeacher_mr(), 
					us.getTeacher_level(), us.getTeacher_pt_mr_type(), us.getTeacher_level_type());
			return tdb_return;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public PTDetailBo getPTDetail(Integer id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public MRDetailBo getMRDetail(Integer id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String complain(Integer id, String number, String type,
			String detail) {
		// TODO Auto-generated method stub
		String hql = "from Complain where complain_user_id="+id+" and complain_reservation_number="+number;
		List<Complain> lc = baseDao.findByHQL(hql);
		if(lc.size() > 0){
			return "You have complain for this reservation";
		}
		else{
			Complain comp = new Complain();
			comp.setComplain_user_id(id);
			comp.setComplain_reservation_number(number);
			comp.setComplain_reservation_type(type);
			comp.setComplain_content(detail);
			baseDao.save(comp);
			return "success for complain";
		}
	}

	@Override
	public List<TeacherBo> search2teacher(String select_str, String school) {
		// TODO Auto-generated method stub
		String[] select_items = select_str.split(",");
		System.out.println(select_items.length);
		String hql = "from VTeacher where user_school='"+school+"'";
		if(select_items.length > 0){
			if(!select_items[0].equals("")){
				hql += " and user_name like '%" + select_items[0] + "%'";
			}
		}
		if(select_items.length > 1){
			if(!select_items[1].equals("")){
				hql += " and teacher_course='" + select_items[1] + "'";
			}
		}
		if(select_items.length > 2){
			if(!select_items[2].equals("")){
				hql += " and (";
				String[] department_items = select_items[2].split("#");
				for(Integer i = 0; i < department_items.length; i++){
					if(i > 0) hql += " or ";
					hql += "teacher_department='" + department_items[i] + "'";
				}
				hql += ")";
			}
		}
		if(select_items.length > 3){
			if(!select_items[3].equals("")){
				hql += " and (";
				String[] grade_items = select_items[3].split("#");
				for(Integer i = 0; i < grade_items.length; i++){
					if(i > 0) hql += " or ";
					hql += "user_grade='" + grade_items[i] + "'";
				}
				hql += ")";
			}
		}
		if(select_items.length > 4){
			if(!select_items[4].equals("")){
				hql += " and (";
				String[] goal_items = select_items[4].split("#");
				for(Integer i = 0; i < goal_items.length; i++){
					if(i > 0) hql += " or ";
					hql += "teacher_goal>='" + goal_items[i] + "'";
				}
				hql += ")";
			}
		}
		hql += " order by user_recommend_index desc";
		List<VTeacher> user = baseDao.findByHQL(hql);
		List<TeacherBo> tb = new ArrayList<TeacherBo>();
		for(Integer i = 0; i < user.size(); i++){
			VTeacher u = user.get(i);
			int teacher_id = u.getUser_id();
			String teacher_name = u.getUser_name();
			String teacher_grade = u.getUser_grade();
			String teacher_department = u.getTeacher_department();
			String teacher_star = u.getTeacher_level();
			String teacher_avatar = COMMON.NulltoAvatar(this.getResource(teacher_id,ResourceType.USER_AVATAR));
			Integer teacher_goal = u.getTeacher_goal();
			Integer teacher_level_type = u.getTeacher_level_type();
			TeacherBo t = new TeacherBo(teacher_id, teacher_name,
					teacher_department, teacher_star,
					teacher_grade, teacher_avatar, teacher_goal, teacher_level_type);
			tb.add(t);
		}
		return tb;
	}


}
